package com.aaa.singleton;




/**
 * @author C-Q
 * @version 1.1.0
 * @Description: volatile保证有序性【防止jvm重排】.volatile保证线程之间的可见性
 * @date 2022/11/8
 */
public class Test01 {
    public static void main(String[] args) throws Exception{
        T t = new T();
        t.start();

        Thread.sleep(2000);
        System.out.println("主线程设置t线程的参数来停止损失");
        //为flag修改为false
        t.setFlag(false);
    }
}
class T extends Thread{
    //可以保证多线程之间的可见性
    private volatile  boolean flag=true;

    public void setFlag(boolean flag){
        this.flag=flag;
    }

    @Override
    public void run() {
        System.out.println("进入run方法");
        while (flag){

        }
    }
}